package com.example.haosiji.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.haosiji.R;
import com.example.haosiji.app.DBManager;
import com.example.haosiji.app.haosijiApp;
import com.example.haosiji.listview.OrderListView;
import com.example.haosiji.listview.OrderListView.DelButtonClickListener;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class myOrder extends Activity{
	
	private OrderListView orderListView;
	private ArrayAdapter<String> mAdapter;
	private List<String> mDatas;
	private haosijiApp mApp = null;
	private long clickTime = 0;
	private ArrayList<Map<String, Object>> listItem;
	
	private TextView isEmpty;
	
	SimpleAdapter adapter;
	
	private MainActivity mParent;

	protected void onCreate(Bundle savedInstanceState) {
			
			super.onCreate(savedInstanceState);
			setContentView(R.layout.myorder);
			mApp = ((haosijiApp) getApplication());	
			
			mParent = (MainActivity)getParent();
			
			isEmpty = mParent.isEmpty;
			isEmpty.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					
				    AlertDialog.Builder builder = new AlertDialog.Builder(myOrder.this);  
				    builder.setMessage("您确定要清空所有数据吗?")  
				           .setCancelable(false)  
				           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  
				               public void onClick(DialogInterface dialog, int id) {  
				                    
				            	listItem.clear();
				               	adapter.notifyDataSetChanged();
				               	try {
									DBManager.deleteAllMyDriver();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}   
				            	   
				               }  
				           })  
				           .setNegativeButton("No", new DialogInterface.OnClickListener() {  
				               public void onClick(DialogInterface dialog, int id) {  
				                    dialog.cancel();  
				               }  
				           });  
				    AlertDialog alert = builder.create();  
					alert.show();
					
				}
			});
	}


	public boolean onTouchEvent(MotionEvent ev){
		orderListView.dismissPopWindow();
		return false;
	}
	
	protected void onResume(){
		super.onResume();
		
		listItem = DBManager.getDriverInfo();
		
		adapter = new SimpleAdapter(this,listItem,R.layout.orderlist,
				new String[]{"name","date","phone","order_id"},
				new int[]{R.id.driver_name,R.id.driver_date,R.id.driver_phone,R.id.order_id});
		
		
		orderListView = (OrderListView)findViewById(R.id.order_listview);
		orderListView.setAdapter(adapter);
		
		orderListView.setSelector(R.drawable.base_background);
		
		
		
		orderListView.setDelButtonClickListener(new DelButtonClickListener(){  
            @Override  
            public void clickHappend(final int position){  
            	
            	HashMap<String,Object> person = (HashMap<String,Object>)adapter.getItem(position);
            	
//            	Toast.makeText(getApplicationContext(), position+"", Toast.LENGTH_SHORT).show();
            	
            	listItem.remove(position);
            	
            	adapter.notifyDataSetChanged();
            	
            	try {
					DBManager.deleteMyDriverById(person.get("order_id").toString());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            	
            	Toast.makeText(getApplicationContext(), "删除成功！", Toast.LENGTH_SHORT).show();
            	
//            	String item = adapter.getItem(position).toString();
            	
//                Toast.makeText(getApplicationContext(), person.get("order_id").toString(), Toast.LENGTH_SHORT).show();
            	
            	
            	
                 
            }  
        });
		
	}
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		orderListView.dismissPopWindow();
	}


	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent){
		if(paramInt == KeyEvent.KEYCODE_BACK){
			
			if(System.currentTimeMillis() - clickTime > 2000){
				Toast.makeText(this, "再按一次退出应用！", Toast.LENGTH_SHORT).show();
				clickTime = System.currentTimeMillis();
			}else{
				mApp.exit();
			}
			
		}
		
		return false;
	}
	
	
	
}
